import scrapy

from scrapy_04_movie.items import MovieItem


class MvSpider(scrapy.Spider):
    name = "mv"
    allowed_domains = ["www.dygod.org"]
    start_urls = ["https://www.dygod.org/html/gndy/china/index.html"]

    def parse(self, response):
        # 第一页的名称，第二页的 img url
        a_list = response.xpath('//div[@class="bd3r"]//ul//b/a[last()]')

        for a in a_list:
            name = (a.xpath('./text()').extract_first())
            href = a.xpath('./@href').extract_first()
            url = 'https://www.dygod.org' + href
            yield scrapy.Request(url, callback=self.parse_second,  meta={'name': name})

    def parse_second(self, response):

        img_url = response.xpath('//div[@id="Zoom"]//img/@src').extract_first()
        movie_item = MovieItem(img=img_url, name=response.meta['name'])
        print('-----> 电影信息  ： ',movie_item)
        yield movie_item
